/*
 * File      : bb_arith.c
 * This file is part of Black Bird Simulation Platform
 * Copyright 2013-2020 BB(Black Bird) Development Team.  
 * All rights reserved.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2013-10-24     Holt         First Version
 */
 
 
#include "bb_arith.h"
/*
 * FUNC		:	select the max data of the two
 *
 * PARAM1	:	input x
 * PARAM1	:	input y
 *
 * RETURN 	:	the max data of x and y
 *				
 */
bb_base bb_arith_max(bb_base x,bb_base y)
{
	return x > y ? x : y;
}

/*
 * FUNC		:	select the min data of the two
 *
 * PARAM1	:	input x
 * PARAM1	:	input y
 *
 * RETURN 	:	the min data of x and y
 *				
 */
bb_base bb_arith_min(bb_base x,bb_base y)
{
	return x > y ? y : x;
}

/*
 * FUNC		:	get the Quotient of x / y
 *
 * PARAM1	:	the dividend x
 * PARAM1	:	the dividor y
 *
 * RETURN 	:	Quotient near the below integer
 *				
 */
bb_base bb_arith_div(bb_base x,bb_base y)
{
	if( -12/5 == -2
		&&	(x < 0) != (y < 0) 
		&& x%y != 0)
		return x/y - 1;
	else
		return x/y;
}
/* 
 * FUNC		:	get the remainder of x % y
 *
 * PARAM1	:	the dividend x
 * PARAM1	:	the dividor y
 *
 * RETURN 	:	remainder
 *				
 */
bb_base bb_arith_mod(bb_base x,bb_base y)
{
	if (-12/5 == -2
	&&	(x < 0) != (y < 0) && x%y != 0)
		return x%y + y;
	else
		return x%y;
}
/*
 * FUNC		:	get the Quotient of x / y
 *
 * PARAM1	:	the dividend x
 * PARAM1	:	the dividor y
 *
 * RETURN 	:	Quotient near the below integer
 *				
 */
bb_base bb_arith_floor(bb_base x,bb_base y)
{
	return bb_arith_div(x, y);
}

/*
 * FUNC		:	get the Quotient of x / y
 *
 * PARAM1	:	the dividend x
 * PARAM1	:	the dividor y
 *
 * RETURN 	:	Quotient upon the high integer
 *				
 */
bb_base bb_arith_ceiling(bb_base x,bb_base y)
{
	return bb_arith_div(x, y) + (x%y != 0);
}